import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

const routes = [
    {
        path: '*',
        redirect: '/home'
    },
    {
        name: 'home',
        component: () => import('./view/home'),
        meta: {
            title: '首页'
        }
    },
    {
        name: 'home',
        path: '/service',
        component: () => import('./view/service'),
        meta: {
            title: '所有服务'
        }
    },
    {
        name: 'home',
        path: '/service/detail',
        component: () => import('./view/service/ServiceDetail'),
        meta: {
            title: '产品介绍'
        }
    },
    {
        name: 'user',
        component: () => import('./view/user'),
        meta: {
            title: '我的'
        }
    },
    {
        name: 'order',
        path: '/order/:status',
        component: () => import('./view/order'),
        props: true,
        meta: {
            title: '我的订单'
        }
    },
    {
        name: 'card',
        component: () => import('./view/card'),
        meta: {
            title: '激活'
        }
    }
];

// add route path
routes.forEach(route => {
    route.path = route.path || '/' + (route.name || '');
});
console.info(routes)
const router = new Router({ routes });

router.beforeEach((to, from, next) => {
    const title = to.meta && to.meta.title;
    if (title) {
        document.title = title;
    }
    next();
});

export {
    router
};